import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '../views/user/Login.vue'
import Register from '../views/user/Register.vue'

import Home from '../views/home/Home.vue'
import Main from '../views/home/Main.vue'
import Pair from '../views/home/Pair.vue'
import WantAdopt from '../views/home/WantAdopt.vue'
import LoveAdopt from '../views/home/LoveAdopt.vue'
import LostFind from '../views/home/LostFind.vue'

import AddInfo from '../views/add/AddInfo.vue'
import AddPair from '../views/add/AddPair.vue'
import AddWantAdopt from '../views/add/AddWantAdopt.vue'
import AddLoveAdopt from '../views/add/AddLoveAdopt.vue'
import AddLostFind from '../views/add/AddLostFind.vue'

Vue.use(VueRouter)

import CategoryItemR from '../components/category/CategoryItemR.vue'
import CategoryItemL from '../components/category/CategoryItemL.vue'
import Category from '../components/category/Category.vue'

import Banner from '../components/Banner.vue'
import MySwiper from '../components/MySwiper.vue'

import PetList from '../components/pet/PetList.vue'
import PetListItemL from '../components/pet/PetListItemL.vue'
import PetListItemR from '../components/pet/PetListItemR.vue'

import AddTypeItem from '../components/add/AddTypeItem.vue'
import AddType from '../components/add/AddType.vue'
import BannerAdd from '../components/add/BannerAdd.vue'

import MsgItemL from '../components/msg/MsgItemL.vue'
import MsgItemR from '../components/msg/MsgItemR.vue'
import MsgList from '../components/msg/MsgList.vue'

import WantAdoptItem from '../components/wantAdopt/WantAdoptItem.vue'
import WantAdoptList from '../components/wantAdopt/WantAdoptList.vue'


Vue.component('CategoryItemR', CategoryItemR)
Vue.component('CategoryItemL', CategoryItemL)
Vue.component('Category', Category)
Vue.component('Banner', Banner)
Vue.component('PetList', PetList)
Vue.component('MySwiper', MySwiper)
Vue.component('PetListItemL', PetListItemL)
Vue.component('PetListItemR', PetListItemR)
Vue.component('AddTypeItem', AddTypeItem)
Vue.component('AddType', AddType)
Vue.component('BannerAdd', BannerAdd)
Vue.component('MsgItemL', MsgItemL)
Vue.component('MsgItemR', MsgItemR)
Vue.component('MsgList', MsgList)
Vue.component('WantAdoptItem', WantAdoptItem)
Vue.component('WantAdoptList', WantAdoptList)

const routes = [
   {
      path: '/',
      redirect: '/login'
   },
   {
      path: '/login',
      component: Login
   },
   {
      path: '/register',
      component: Register
   },{
      path: '/main',
      component:Main,
      redirect: '/home',
      children:[
         {path: '/home',component: Home},
         {path: '/pair',component: Pair},
         {path: '/wantAdopt',component: WantAdopt},
         {path: '/loveAdopt',component: LoveAdopt},
         {path: '/lostFind',component: LostFind}
      ]
   },
   {
      path: '/addInfo',
      component: AddInfo,
      redirect: '/addInfo/pair',
      children:[
         {path: '/addInfo/pair',component: AddPair},
         {path: '/addInfo/wantAdopt',component: AddWantAdopt},
         {path: '/addInfo/loveAdopt',component: AddLoveAdopt},
         {path: '/addInfo/lostFind',component: AddLostFind},
      ]
   }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to,from,next)=>{
   // to：将要访问
   // from：从哪访问
   // next：接着干的事 next(url) 重定向到url上，next()继续访问to的路径
   if((to.path == '/login') || (to.path == '/register')) return next();
   // 获取user
   const userFlag = window.sessionStorage.getItem('userId'); // 取出当前用户
   if(!userFlag) return next('/login'); // 无值，返回登录页
   next(); //符合要求 放行
 
 })
 

export default router
